﻿using ArxOne.MrAdvice.Advice;
using HAF.LogUtility;
using System;
using System.Diagnostics;
using System.Linq;

namespace WcfAdvice.AOP
{
    /// <summary>
    /// 日志拦截器、特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class LoggerAttribute : Attribute, IMethodAdvice
    {
        public void Advise(MethodAdviceContext context)
        {
            //OperationContext.Current.RequestContext.
            var key = GetKey(context);
            if (key != null) { }
            LogHelper.WriteErrorLogEx(context.Arguments[0].ToString());
            LogHelper.WriteErrorLogEx(context.TargetName);
            Trace.WriteLine($"<==========before proceed {context.TargetName}==========>");
            context.Proceed();
            Trace.WriteLine("<==========end proceed==========>");
            Debug.WriteLine("啊哈");
            //窗体程序不能使用Console.writeline，只有控制台程序才能用
            //Console.WriteLine("啊哈");
        }

        private string GetKey(MethodAdviceContext context)
        {
            var array = context.TargetMethod.GetParameters();

            var key = array.Select(x => { return context.Arguments[x.Position].ToString(); }).ToArray();

            //var cacheKey = $"{context.Target.ToString()}|{context.TargetName}|{string.Join("", array.Select(x => x.Name))}|{string.Join("", key)}".GetMd5();

            return null;

        }
    }
}
